package com.kevin.Code.OfferReviewVersion2;

/**
 * @author Vinlee Xiao
 * @Classname MaxAreaOfIsland
 * @Description Leetcode 695. 岛屿的最大面积 中等难度 二刷题
 * @Date 2022/1/11 10:09
 * @Version 1.0
 */
public class MaxAreaOfIsland {

    /**
     * 思路:深度优先遍历
     * 求出最大的岛屿面积
     *
     * @param grid
     * @return
     */
    public int maxAreaOfIsland(int[][] grid) {

        int row = grid.length;
        int col = grid[0].length;
        int result = Integer.MIN_VALUE;

        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                result = Math.max(result, dfs(grid, i, j));
            }
        }

        return result;
    }

    /**
     * @param grid
     * @return
     */
    public int dfs(int[][] grid, int i, int j) {
        //递归中止条件
        if (i < 0 || i > grid.length - 1 || j < 0 || j > grid[0].length - 1 || grid[i][j] == 0) {
            return 0;
        }

        return 1 + dfs(grid, i, j - 1) + dfs(grid, i, j + 1) + dfs(grid, i - 1, j) + dfs(grid, i + 1, j);
    }
}
